/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.info.template.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.FreemarkerHelper;
import com.apache.info.util.FileOperate;

/**
 * description:  生成Action实现类
 * @author dyhou 创建时间：2016-9-30  
 * @Copyright(c)2014:北京中百信软件技术有限公司
 */
public class ActionTempManager extends SupperTemplateManager {

	public ActionTempManager(PubSortInfoManager sortInfoManager, PubMetadataManager metadataManager) {
		this.init(sortInfoManager, metadataManager);
	}

	public boolean createAction(Map<String, Object> param) {
		boolean mark = false;
		List<Map<String, Object>> list = (List<Map<String, Object>>) param.get("sortList");
		String jarPath = String.valueOf(param.get("packPath"));
		String prefix = String.valueOf(param.get("projectName")) + "/src/main/";
		String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix + "java/" + jarPath.replace(".", "/")
				+ "/controller/";
		//		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.zbxsoft.utr中的utr
		String sysName = String.valueOf(param.get("sysCode"));
		FileOperate.getInstance().newCreateFolder(xmlPath);
		String restfullClassName = "Restfull" + FileOperate.getInstance().toUpperFirst(sysName) + "Action";
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("restfullClassName", restfullClassName);
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		try {
			String result = FreemarkerHelper.instance().getTemplateStr("controller/RestfullAction.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + restfullClassName + ".java", result);
			result = FreemarkerHelper.instance().getTemplateStr("controller/SuperAction.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "UnitySupperAction.java", result);
			for (Map<String, Object> map : list) {
				String reqObjName = FileOperate.getInstance().toCamelNamed(String.valueOf(map.get("sortCode")));
				data.put("reqObjName", reqObjName);
				data.put("sort", map);
				result = FreemarkerHelper.instance().getTemplateStr("controller/ObjAction.inc", data);
				FileOperate.getInstance().newCreateFile(xmlPath + String.valueOf(map.get("objName")) + "Action.java",
						result);
			}
			mark = true;
		} catch (Exception e) {
			log.error("生成action失败", e);
		}
		return mark;
	}
}
